﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;

namespace ConfExpert.Forms.Rapor
{
    public partial class CC_Sayim : DevExpress.XtraReports.UI.XtraReport
    {
        DataTable dt;
        public CC_Sayim(int BoxNo)
        {
            InitializeComponent();

            dt = Session.DataManager.ExecuteDataTableByQuery("SELECT db.Barcode,db.Model,db.Colour,db.Tailles,s.BoxNo,count(*) Qty FROM CC_Barkod_Sayim s LEFT JOIN CC_Barkod_DB db on s.Barcode = db.Barcode where BoxNo = " + BoxNo.ToStr() + "group by db.Barcode,db.Model,db.Colour,db.Tailles,s.BoxNo");
            Bind(dt);
        }

        public CC_Sayim(string BoxNoS)
        {
            InitializeComponent();

            dt = Session.DataManager.ExecuteDataTableByQuery("SELECT db.Barcode,db.Model,db.Colour,db.Tailles,s.BoxNo,count(*) Qty FROM CC_Barkod_Sayim s LEFT JOIN CC_Barkod_DB db on s.Barcode = db.Barcode where BoxNo in( " + BoxNoS + ") group by db.Barcode,db.Model,db.Colour,db.Tailles,s.BoxNo order by boxno");
            Bind(dt);
        }

        private void Bind(DataTable dt)
        {
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            dt.TableName = "dt";
            //xrBoxNo.Text = BoxNo.ToStr();
            xrBoxNo.Text = dt.Compute("MAX(BoxNo)", "").ToStr();

            tcTotal.Text = dt.Compute("sum(Qty)", "").ToStr();
            this.DataMember = "dt";
            this.DataSource = ds;

            tcColour.DataBindings.Add("Text", null, "dt.Colour");
            tcBarcode.DataBindings.Add("Text", null, "dt.Barcode");
            tcModel.DataBindings.Add("Text", null, "dt.Model");
            tcQty.DataBindings.Add("Text", null, "dt.Qty");
            tcTailles.DataBindings.Add("Text", null, "dt.Tailles");

            MakeResizable(tcColour);
            MakeResizable(tcModel);
            MakeResizable(tcQty);
            MakeResizable(tcTailles);
        }


        #region MakeResizable
        Dictionary<string, Font> defaultFonts = new Dictionary<string, Font>();
        void MakeResizable(params XRControl[] controls)
        {
            foreach (XRTableCell cell in controls)
            {
                defaultFonts.Add(cell.Name, new System.Drawing.Font("Calibri", 11F));
                cell.BeforePrint += new PrintEventHandler(cell_BeforePrint);
            }
        }

        void cell_BeforePrint(object sender, PrintEventArgs e)
        {
            Resize((XRControl)sender);
        }

        private void Resize(XRControl cell)
        {
            //Resize ı table'larda düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            float factor = 10;

            Font defaultFont = defaultFonts[cell.Name];
            float newSize = defaultFont.Size;

            float stringWidth = gr.MeasureString(cell.Text, defaultFont).Width;
            float cellWidth = cell.WidthF;

            if (cellWidth < stringWidth * factor)
                newSize = defaultFont.Size * cell.Width / stringWidth / factor;

            Font newFont = new Font(defaultFont.Name, newSize, defaultFont.Style, defaultFont.Unit, defaultFont.GdiCharSet, defaultFont.GdiVerticalFont);

            cell.Font = newFont;

            gr.Dispose();
        }
        #endregion
    }
}